import { login, register, getUser, serchUser, findallshoper } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'
import { getRoleJson, _getRoleList } from '../../utils/permission'
import ElementUI from 'element-ui';

const getDefaultState = () => {
  return {
    token: getToken(),
    name: '',
    avatar: '',
    quanxian: "",
    data: [],
    list: [],
    allshoper: [],
  }
}
const state = getDefaultState()
const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    let { account, password } = userInfo
    return new Promise((resolve, reject) => {
      login({ account: account.trim(), password: password }).then(response => {
        if (Object.keys(response).length != 0) {
          console.log(response.data, "data", response, 'response');
          // this.state.user.data=response.data
          // console.log(this.state.user.data,156488548948165423);
          if (response.data[0].account == "adminsuper") {
            localStorage.setItem("Jurisdiction", response.data[0].position)
            localStorage.setItem("token", response.token)

            console.log(response.data[0]._id);
            resolve()
          } else if (response.data[0].account == "admin") {
            localStorage.setItem("Jurisdiction", response.data[0].position)
            localStorage.setItem("token", response.token)
            commit('SET_TOKEN', response.token)
            setToken(response.token)
            resolve()

          } else {
            if (response.data[0].status == 1) {
              console.log(1234);
              ElementUI.Message({
                message: '该账号正在申请中',
                type: 'success'
              });
              window.location.reload()
            } else if (response.data[0].isBan) {
              ElementUI.Message({
                message: '该账号已冻结',
                type: 'success'
              });
              window.location.reload()
              
            } else {
              localStorage.setItem("Jurisdiction", response.data[0].position)
              localStorage.setItem("token", response.token)
              localStorage.setItem('ID', response.data[0]._id)
              commit('SET_TOKEN', response.token)
              setToken(response.token)
              resolve()
            }


          }


        } else {
          window.alert("账户不存在！")
        }
      }).catch(error => {
        reject(error)
      })
    })
  },
  // 注册
  register({ commit }, userInfo) {
    return new Promise((resolve, reject) => {
      register(userInfo).then((res) => {
        if (Object.keys(res).length != 0) {
          let fig = window.confirm("注册成功 正在申请通过！ 是否跳转登录页面")
          if (fig) {
            location.href = "http://localhost:3001";
          } else {
            window.location.reload()
          }
        } else {
          window.alert("该账号被注册")
          window.location.reload()
        }

      })
    })

  },

  getUserActions() {
    getUser().then((res) => {
      this.state.user.list = res
      // console.log(this.state.user,"this.state.user");
    })
  },
  serachUser({ commit }, payload) {
    console.log(payload, "payload");
    serchUser(payload).then((res) => {
      console.log(res, "res");
      this.state.user.list = res
      console.log(this.state.user.list, " this.state.user.list");
    })

  },




  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo(state.token).then(response => {
        const { data } = response
        if (!data) {
          return reject('Verification failed, please Login again.')
        }
        const { name, avatar } = data
        commit('SET_AVATAR', avatar)
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  findallshoper() {
    console.log('进入')
    findallshoper().then(res => {
      console.log(res)
      this.state.user.allshoper = res;
      console.log(this.state.user.allshoper)
    })
  }

  // remove token

}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

